<template>
  <div>
    <el-dialog
      title="新增摄像头监控"
      :visible.sync="dialogVisible"
      width="800px"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="formRules" label-width="130px">
        <el-form-item label="CRON表达式" prop="cron">
          <el-input v-model="form.cron" clearable :maxlength="100" placeholder="请生成CRON表达式">
            <el-button slot="append" @click="addCron"> 生成表达式</el-button>
          </el-input>
        </el-form-item>
        <dt-Vcrontab ref="vcrontabRef" v-model.trim="form.cron" />
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="interfaceServiceConfirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { setBatchMonitorTimer } from '@/api/groupCameraSynchronization'

export default {
  name: 'SynchronousGroupCamera',
  data() {
    return {
      list: null,
      dialogVisible: false,
      form: {},
      formRules: {
        cron: { required: true, message: '请生成CRON表达式', trigger: 'blur' }
      }
    }
  },
  methods: {
    addCron() {
      this.$refs.vcrontabRef.showDialog()
    },
    close() {
      this.$refs.form.resetFields()
      this.list = null
    },
    // 确定按钮
    interfaceServiceConfirm() {
      if (this.list.length === 0) return this.$emit('请勾选要同步的数据')
      this.$refs['form'].validate((v) => {
        if (v) {
          const q = this.list.map(item => {
            return {
              cron: this.form.cron,
              decodeTag: item.decodeTag,
              indexCode: item.indexCode,
              name: item.name,
              regionIndexCode: item.regionIndexCode
            }
          })
          setBatchMonitorTimer(q).then(res => {
            if (res.code === '00000') {
              this.list = null
              this.dialogVisible = false
              this.$message.success(res.message)
              this.$emit('getList')
            }
          }).finally(r => {
            this.list = null
            this.dialogVisible = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.create-service {
  ::v-deep .el-dialog__body {
    padding: 0 20px;
    height: 500px;
    overflow-y: auto;
  }
}
</style>

